/**
 * My simple header template from child directory
 * SnakeCoordinator.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
package FourthTry;

/*
 * #%L
 * Ability-tester
 * %%
 * Copyright (C) 2014 AmazingSERKU
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 10.01.14
 * Time: 23:29
 * To change this template use File | Settings | File Templates.
 */
public class SnakeCoordinator implements ICoordinator {

    private Direction mainDirection;
    private Direction auxiliaryDirection;
    private Coordinates startCoordinates;
    private IArea area;

    public SnakeCoordinator(IArea area, Coordinates startCoordinates) {
        this.area = area;
        this.mainDirection = Direction.RIGHT;
        this.auxiliaryDirection = Direction.DOWN;
        this.startCoordinates = startCoordinates;
    }

    public SnakeCoordinator(IArea area, Direction mainDirection, Direction auxiliaryDirection, Coordinates startCoordinates) {
        this.area = area;
        this.mainDirection = mainDirection;
        this.auxiliaryDirection = auxiliaryDirection;
        this.startCoordinates = startCoordinates;
    }

    @Override
    public ICoordinator copyState(ICoordinator coordinator) {
        this.setArea(coordinator.getArea());
        return this;
    }

    @Override
    public CoordinatorCell getNextCell(Coordinates coordinates) {
        Coordinates temp = new Coordinates(coordinates);
        mainDirection.changeCoordinate(temp);
        Cell cell = area.getCell(temp);
        if (cell.isAbroad()) {
            temp = new Coordinates(coordinates);
            auxiliaryDirection.changeCoordinate(temp);
            cell = area.getCell(temp);
            if (cell.isAbroad()) {
                return new CoordinatorCell(cell, true);
            }
            mainDirection = mainDirection.opposite();
        }
        return new CoordinatorCell(cell, false);
    }

    @Override
    public IArea getArea() {
        return area;
    }

    @Override
    public void setArea(IArea area) {
        this.area = area;
    }
}
